/************************************************************************************/
/*  Program name CAPGLIMP_DAILY_CONV_TYPES.sql                                      */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 24 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER GL_DAILY_CONVERSION_TYPES_CT1 DISABLE;
ALTER TRIGGER GMF_GL_DAILY_CNV_TYPES_BIUR_TG DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_DAILY_CONV_TYPES';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_DAILY_CONVERSION_TYPES;
	
	CURSOR Cur_C IS
	SELECT
		CDC.*
	FROM
		CGL_DAILY_CONVERSION_TYPES		CDC,
		CRST_GL_DAILY_CONV_TYPES_MAP	CGD
	WHERE
		CDC.CONVERSION_TYPE	=	CGD.CONVERSION_TYPE;
	
	lv_Next_Seq_ID		NUMBER;
	
BEGIN
	UPDATE
		GL_DAILY_CONVERSION_TYPES
	SET	
		USER_CONVERSION_TYPE	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(USER_CONVERSION_TYPE,1,4), USER_CONVERSION_TYPE)
	WHERE CONVERSION_TYPE IN
		(
		SELECT 
			CONVERSION_TYPE
		FROM 
			CRST_GL_DAILY_CONVERSION_TYPES
		);
		
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	GL_DAILY_CONVERSION_TYPES_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;
			
			INSERT INTO CRST_GL_DAILY_CONV_TYPES_MAP
			VALUES
			(
			C1.CONVERSION_TYPE,
			CAPBRIMP_MAPPING.RenameStr(SUBSTR(C1.USER_CONVERSION_TYPE,1,4), C1.USER_CONVERSION_TYPE),
			TO_CHAR(lv_Next_Seq_ID),
			C1.USER_CONVERSION_TYPE
			);

			INSERT INTO GL_DAILY_CONVERSION_TYPES
			VALUES
			(
			TO_CHAR(lv_Next_Seq_ID),
			C1.USER_CONVERSION_TYPE,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.LAST_UPDATE_LOGIN,
			C1.DESCRIPTION,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.CONTEXT
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	FOR C2 IN Cur_C LOOP
		BEGIN
			INSERT INTO CGL_DAILY_CONVERSION_TYPES
			VALUES
			(
			CAPBRIMP_MAPPING.GetGLConversionTypeMapping(C2.CONVERSION_TYPE),
			CAPBRIMP_MAPPING.GetOrgMapping(C2.S_ORG_ID),
			CAPBRIMP_MAPPING.GetBGMapping(C2.S_BUSINESS_GROUP_ID)
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/

ALTER TRIGGER GL_DAILY_CONVERSION_TYPES_CT1 ENABLE;
ALTER TRIGGER GMF_GL_DAILY_CNV_TYPES_BIUR_TG ENABLE;